package com.wht.arthas.controller;

import com.wht.arthas.model.User;
import com.wht.arthas.service.UserService;
import com.wht.arthas.util.IdUtils;
import com.wht.arthas.util.IpUtils;
import com.wht.test.App;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author haoting.wang
 * @date 2020/8/4 7:12 下午
 **/
@RestController
@RequestMapping("demo")
public class DemoController {

    private static final Logger logger = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    private UserService userService;

    @GetMapping("/user/{id}")
    public User findById(@PathVariable("id") Integer id) {

        if (id == null) {
            throw new RuntimeException("id 为空");
        }
        if (id < 0) {
            throw new RuntimeException("id 非法");
        }
        return userService.findById(id);
    }

    @GetMapping("/user")
    public Collection<User> find() {

        return userService.getAllUser();
    }

    @PutMapping("/user")
    public ResponseEntity<String> update(@RequestBody User user) {

        if (userService.findById(user.getId()) == null) {
            return ResponseEntity.ok("该用户不存在");
        }
        userService.update(user);

        return ResponseEntity.ok("修改成功");
    }

    @PostMapping("/user")
    public ResponseEntity<String> add(@RequestBody User user) {

        if (userService.findById(user.getId()) != null) {
            return ResponseEntity.ok("该用户已存在");
        }
        userService.save(user);

        return ResponseEntity.ok("新增成功");
    }

    @GetMapping("/logger")
    public void testLogger() {

        logger.debug("hello arthas, im debug");

        logger.info("hello arthas,im info");

        logger.warn("hello arthas,im warn");

    }

    @GetMapping("exception/{num}")
    public ResponseEntity<String> testException(@PathVariable("num") Integer num) {

        if (num == 10) {
            throw new RuntimeException("系统异常");
        }
        return ResponseEntity.ok("调用成功");
    }

    @GetMapping("/hotfix/{id}")
    public ResponseEntity<User> hotfix(@PathVariable("id") Integer id) {

        Integer newId = IdUtils.fixId(id);

        return ResponseEntity.ok(userService.findById(newId));
    }

    @GetMapping("/conflict")
    public String conflict() {

        return App.test("hello");
    }

    @GetMapping("/trace")
    public void trace(HttpServletRequest request) {

        for(int i=0 ;i<10;i++){
            String ipAddress = IpUtils.getIpAddress(request);

            String hostIp = IpUtils.getHostIp();

            logger.info("数据请求从:{}发送到:{}",ipAddress,hostIp);
        }
        userService.sleep();

    }
    @GetMapping("/thread")
    public Long thread(){

        long result=0;
        for(long i = 0;i<10000000000L;i++){
            result += i;
        }

        return result;
    }

}
